package com.titan.core.utils.object;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections4.set.ListOrderedSet;

import com.titan.core.utils.data.AssertUtils;
import com.titan.core.utils.data.FormatUtils;


/**
 * @author 周成功
 * @email 86898837@qq.com
 * @date 2020年02月14日
 */
public class ListSet<T> implements java.io.Serializable, Cloneable {
    private ListOrderedSet<T> data = null;
    
    public <T> ListSet() {
        data = new ListOrderedSet();
    }
    public <T> ListSet(Set set, List list) {
        data = ListOrderedSet.listOrderedSet(set, list);
    }
    public static <T> ListSet<T> $() {
        return new ListSet<T>();
    }
    public static <T> ListSet<T> $(int size) {
        ListSet<T> bean = new ListSet<T>(new HashSet(size), new ArrayList<T>(size));
        return bean;
    }
    public static <T> ListSet<T> $(final Set set, final List list) {
        return new ListSet<T>(set, list);
    }
    public static <T> ListSet<T> $Lock(int size) {
        List<T> list = Collections.synchronizedList(new ArrayList(size));
        Set<T> set = Collections.synchronizedSet(new HashSet(size));
        ListSet<T> bean = new ListSet<T>(set, list);
        return bean;
    }
    
    public ListSet<T> clone() {
        ListSet<T> dataNew = ListSet.$(data.size());
        for (int i = 0; i < data.size(); i++) {
            T value = data.get(i);
            dataNew.add(value);
        }
        return dataNew;
    }    
    
    public boolean add(final T object) {
        return data.add(object);
    }
    public void add(final int index, final T object) {
        data.add(index, object);
    }
    public boolean addAll(final T[] object) {
        if(object==null || object.length==0) {
            return true;
        }
        for (int i = 0; i < object.length; i++) {
            data.add(object[i]);
        }
        return true;
    }
    public boolean addAll(final Collection<T> object) {
        if(object==null || object.size()==0) {
            return true;
        }
        return data.addAll(object);
    }    
    public boolean addAll(final ListSet<T> object) {
        if(object==null || object.size()==0) {
            return true;
        }
        return data.addAll(object.toList());
    }    

    
    public T get(final int index) {
        return data.get(index);
    }
    
    public int indexOf(final T object) {
        return data.indexOf(object);
    }

    public boolean contains(final T object) {
        return data.contains(object);
    }
    
    public T remove(final int index) {
        return data.remove(index);
    }
    public boolean removeByKey(final T object) {
        return data.remove(object);
    }
    public boolean isContains(final T object) {
        T[] items = (T[])data.toArray();
        return AssertUtils.isContains(object, items);
    }
    public void clear() {
        data.clear();
    }
    public int size() {
        return data.size();
    }
    
    
    public Set<T> toSet() {
        return data;
    }
    public List<T> toList() {
        return data.asList();
    }
    public T[] toArray() {
        return (T[])data.toArray();
    }
    public T[] toArray(final T a[]) {
        return data.toArray(a);
    }
    public String toString() {
        if(data==null) {
            return null;
        }
        StringBuilder buff = new StringBuilder(1024);
        List<T> list = data.asList();
        for (int i = 0; i < list.size(); i++) {
            Object obj = list.get(i);
            if(i>0) {
                buff.append(",");
            }
            buff.append(FormatUtils.toString(obj));
        }
        return buff.toString();
    }
}
